<?php

namespace App\Http\Controllers\Mobile;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\UserCoupon;
use App\Models\Coupon;

use App\Interfaces\CouponService;
use App\Interfaces\PasswordCouponService;

use App\Exceptions\Front\CouponException;
use App\Exceptions\Interfaces\CouponServiceException;
use App\Exceptions\Interfaces\PasswordCouponServiceException;

use Auth,DB,Log;

class MyCouponController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, CouponService $couponService)
    {

        $search = ['type_status'=>'1'];

        $types = ['1'=>'未使用', '2'=>'已使用', '3'=>'已过期'];

        $user = Auth::user();

        $user_id = $user->id;


        $type_status = @$request->type_status;

        if (!empty($type_status)) {

            $search['type_status'] = $type_status;
        }//end if()

// echo '<BR>search:';print_r($search);

        $user_coupon = $couponService->getUserCoupon($user_id, $search);

// dd($user_coupon->toArray());
        return view('mobile.my.coupon', compact('user_coupon', 'types', 'search'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * 领取优惠券
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, CouponService $couponService)
    {

        $return = ['status'=>1, 'message'=>'领取成功', 'data'=>''];

        try {

            $user = Auth::user();

            $user_id = $user->id;

            $coupon_id = $request->coupon_id;


            if (empty($coupon_id)) {

                $return['status'] = 0;

                $return['message'] = '缺少优惠券信息';

                return $return;
            }//end if()

            $user_coupon_id = $couponService->receiveCoupon($user_id, $coupon_id);

            $return['data'] = $user_coupon_id;

        } catch (CouponServiceException $e) {

            $return['status'] = 0;

            $return['message'] = $e->getMessage();
        }

        return $return;
    }

    /**
     * 调用优惠券服务，测试优惠券的使用
     *
     * @param  int  $coupon_id      用户优惠券id
     * @return \Illuminate\Http\Response
     */
    public function show($coupon_id, CouponService $couponService)
    {
        // dd('测试使用优惠券');
        $user = Auth::user();
        $user_id = $user->id;
        //$user_id = 198859;
        //$coupon_id = 15;
        $result = $couponService->useCoupon($user_id, $coupon_id);

        dd('result:'.$result);}

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除优惠券
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id, CouponService $couponService)
    {

        $return = ['status'=>1, 'message'=>'', 'data'=>''];

        $user = Auth::user();

        $user_id = $user->id;

        $return = ['status'=>1, 'message'=>'', 'data'=>''];

        try {

            //$user_coupon = UserCoupon::where('user_id', $user_id)->where('id', $id)->delete();

            $user_coupon = $couponService->destroyMyCoupon($user_id, $id);

            $return['data'] = $user_coupon;

        } catch (CouponServiceException $e) {

            $return['status'] = 0;

            $return['message'] = $e->getMessage();

        }

        return $return;
    }


    /**
     * 根据查询条件统计优惠券数量
     *
     * @return \Illuminate\Http\Response
     */
    /*public function statisticsCount(Request $request, CouponService $couponService)
    {

        $user = Auth::user();

        $user_id = $user->id;

        //$user_id = 198859;

        $query = UserCoupon::leftJoin('coupon', 'user_coupon.coupon_id', '=', 'coupon.id')

                ->select('user_coupon.id as user_coupon_id', 'user_coupon.created_at as user_coupon_created_at', 'user_coupon.deleted_at as user_coupon_deleted_at', 'coupon.*');

        $query = $query->whereNull('user_coupon.deleted_at');

        $query = $query->where('user_id', $user_id);



        $user_coupon = $query->get();

        $amount = $user_coupon->count();

        return $amount;

    }//end func statisticsCount()*/



    /**
     * 激活优惠口令
     * @param  [type] $password  [口令]
     * @return [type]            [description]
     */
    public function activationPwd($password, PasswordCouponService $passwordCouponService, CouponService $couponService, Request $request)
    {

        $return = ['status'=>1, 'message'=>'激活成功', 'data'=>''];

        $user = Auth::user();

        if ($user) {

            $user_id = $user->id;
        }else{

            $return['status'] = 0;

            $return['message'] = '当前未登录，请登录';

            return $return;
        }


        $password = @$request->pwd_code;

        if ($password!=='9元游福塔') {

            $return['status'] = 0;

            $return['message'] = '口令不正确';

            return $return;
        }

        $password_status = 2;

        $use_time = date('Y-m-d H:i:s');


        DB::beginTransaction();//开启事务,开始批量操作

        try {

            $coupon_id = env('COUPON_ID','0');

            //第一步：将口令对应的优惠券与用户进行关联
            $result = $couponService->receiveCoupon($user_id, $coupon_id);          //该优惠券id为事先确定好的

        }catch (CouponServiceException $e){

            DB::rollback();

            $return['status'] = 0;

            // $return['message'] = $e->getMessage();

            switch ($e->getMessage()) {

                case '优惠券不存在':

                    $return['message'] = '该口令对应的优惠券不存在';

                    break;

                case '该优惠券已被领完':

                    $return['message'] = '该口令对应的优惠券已被领完';

                    break;

                case '优惠券已过期':

                    $return['message'] = '该口令对应的优惠券已过期';

                    break;

                case '该优惠券已经领取过了':

                    $return['message'] = '该口令对应的优惠券您已经领取过了';

                    break;
            }
        }catch (PasswordCouponServiceException $e){

            DB::rollback();

            $return['status'] = 0;

            $return['message'] = $e->getMessage();
        }

        DB::commit();

        return $return;
    }
}
